<?php


namespace App\Repositories;


use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;

class InsideUserRepository
{
    /**
     * 列表
     * @param $userCode int 用户编号
     * @param $phone int 手机号
     * @param $page int 分页条目
     * @param $perPage int 分页数量
     */
    public function list($userCode,$phone,$page,$perPage)
    {
        $list = app('model_insider_user')
            ->with([
                'user_data' => function ($query) {
                    $query->select(['user_id', 'nickname', 'user_code']);
                },
                'user'      => function ($query) {
                    $query->select(['user_id', 'mobile']);
                },
                'admin'      => function ($query) {
                    $query->select(['admin_id', 'admin_name']);
                },
            ])
            ->where('deleted',0);
        if (!empty($userCode)) {
            $list = $list->whereHas('user_data', function ($query) use ($userCode) {
                $query->where('user_code', '=', $userCode);
            });
        }
        if (!empty($phone)) {
            $list = $list->whereHas('user', function ($query) use ($phone) {
                $query->where('mobile', '=', $phone);
            });
        }
        $list = $list->paginate($perPage, ['*'], '', $page);

        return $list;
    }

    /**
     * 新增或修改
     * @param $param
     */
    public function addOrEdit($param)
    {
        $userId = app('model_user_data')->where('user_code', $param['user_code'])->value('user_id');

        if (empty($userId)) {
            throw new BasicException(10010, '用户编号不存在');
        }
        $data = [
            'user_id'           => $userId,
            'remark_content'    => $param['remark_content'],
            'admin_id'          => $param['admin_id'],
            'add_time'          => date('Y-m-d H:i:s')
        ];
        $officialCompanyUser = app('model_insider_user')
            ->where('user_id', $userId)
            ->where('deleted',0)
            ->first();

        if ($officialCompanyUser) {
            throw new BasicException(10010, '已经是官方内部用户');
        }

        //已有的提现直接驳回
        DB::beginTransaction();
        try {

            app('model_insider_user')->create($data);

//            $idArr = app('model_user_withdraw')
//                ->where('platform_status',1)
//                ->where('user_id',$userId)
//                ->pluck('id')->toArray();
//            $res = app('repo_user_withdraw')->batchCheck($idArr, 2, '添加内部账号拒绝提现');
//
//            if (!$res) {
//                throw new BasicException(0,'添加内部账号出错');
//            }

            DB::commit();
            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException($e->getCode(), $e->getMessage());
        }

//        if (!isset($param['id'])) {
//            $officialCompanyUser = app('model_insider_user')->where('user_id', $param['user_id'])->first();
//
//            if ($officialCompanyUser) {
//                throw new BasicException(10010, '已经是官方内部用户');
//            }
//            return app('model_insider_user')->create($data);
//        } else {
//            return app('model_insider_user')->where('id',$param['id'])->update($data);
//        }
    }

    /**
     * 删除内部信息
     * @param $ids array 主键集合
     */
    public function deleteInside($ids)
    {
        return app('model_insider_user')->whereIn('id',$ids)->update(['deleted'=>1]);
    }
}